package codeMonkeyClock;

import java.util.*;
import java.text.*;

/**
 *  Clock - class to access the system clock time and add a random offset, upto 15 minutes fast
 *  
 *  Copyright (C) 2013  Samantha Lycett
 *  
 *  This file is part of CodeMonkeyClock.
 *
 *  CodeMonkeyClock is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  CodeMonkeyClock is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CodeMonkeyClock.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 * @author  Samantha Lycett
 * @created 13 June 2013
 */
public class Clock {

	/////////////////////////////////////////////////////////////////////
	// Class methods
	// Uses Singleton design pattern
	/////////////////////////////////////////////////////////////////////
	
	private static Clock thisClock = new Clock();
	
	public static Clock getClock() {
		return thisClock;
	}
	
	/////////////////////////////////////////////////////////////////////
	// Instance variables

	private final double 	 maxOffset 			= 1000*60*15;			// maximum of 15 minutes fast
	private long 		 	 offset_millis	   	= 1000*60;				// initial value of 1 minute fast (overwritten when called initialise)
	private SimpleDateFormat hhmmss 			= new SimpleDateFormat("HH:mm:ss");
	private SimpleDateFormat HH					= new SimpleDateFormat("HH");
	private SimpleDateFormat MM					= new SimpleDateFormat("mm");
	private SimpleDateFormat SS					= new SimpleDateFormat("ss");
	private Date			 currentDate;
	private Date			 monkeyDate;
	
	// private constructor for Singleton design pattern
	private Clock() {
		initialise();
		tick();
	}
		
	/////////////////////////////////////////////////////////////////
	// public instance methods
	
	/**
	 * call this before every call to a get method
	 */
	public void tick() {
		//System.out.println("Tick");
		long t1 	 = System.currentTimeMillis();
		currentDate  = new Date(t1);
		monkeyDate   = new Date(t1 + offset_millis);
	}
	
	public String getRealTime_hhmmss() {
		
		//  http://www.tutorialspoint.com/java/java_date_time.htm
		//  Date dNow = new Date( );
	    //  SimpleDateFormat ft = 
	    //  new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
		//
	    //  System.out.println("Current Date: " + ft.format(dNow));
		
		return( hhmmss.format(currentDate) );
	}
	
	public String getMonkeyTime_hhmmss() {
		return( hhmmss.format(monkeyDate));
	}
	
	
	public int[] getRealTime_as_ints() {
		
		int[] hms 		= new int[3];
		hms[0]	  		= getHour(currentDate);
		hms[1]	  		= getMinutes(currentDate);
		hms[2]	  		= getSeconds(currentDate);
		return ( hms );
	}
	
	public int[] getMonkeyTime_as_ints() {
		int[] hms 		= new int[3];
		hms[0]	  		= getHour(monkeyDate);
		hms[1]	  		= getMinutes(monkeyDate);
		hms[2]	  		= getSeconds(monkeyDate);
		return ( hms );
	}
	
	
	/////////////////////////////////////////////////////////////////////////
	// private instance methods

	private void initialise() {
		//System.out.println("Initialising offset");
		offset_millis = (long)Math.round( Math.random()*maxOffset );
	}
	
	private int getHour(Date d) {
		String txt = HH.format(d);
		return( Integer.parseInt(txt) );
	}
	
	private int getMinutes(Date d) {
		String txt = MM.format(d);
		return( Integer.parseInt(txt) );
	}
	
	private int getSeconds(Date d) {
		String txt = SS.format(d);
		return( Integer.parseInt(txt) );
	}
	
	//////////////////////////////////////////////////////////////////////////
	
}
